﻿#include "prosetpage.h"
#include "ui_prosetpage.h"
#include <QDir>
#include <QFileDialog>
#include <QDebug>
ProSetPage::ProSetPage(QWidget *parent) :
    QWizardPage(parent),
    ui(new Ui::ProSetPage)
{
    ui->setupUi(this);
    registerField("proPath", ui->lblPath);
    registerField("proName*", ui->lblName);
    connect(ui->lblName, &QLineEdit::textEdited, this, &ProSetPage::completeChanged);
    connect(ui->lblPath, &QLineEdit::textEdited, this, &ProSetPage::completeChanged);
    QString curPath = QDir::currentPath();
    ui->lblPath->setText(curPath);
    ui->lblPath->setCursorPosition(ui->lblPath->text().size());
    ui->lblName->setClearButtonEnabled(true);
    ui->lblPath->setClearButtonEnabled(true);
}

ProSetPage::~ProSetPage()
{
    delete ui;
}

bool ProSetPage::isComplete()
{
    if(ui->lblName->text() == "" || ui->lblPath->text() == ""){
        return false;
    }
    //判断文件夹是否合理
    QDir dir(ui->lblPath->text());
    if(!dir.exists())
    {
        //qDebug()<<"file path is not exists" << endl;
        ui->tips->setText("project path is not exists");
        return false;
    }
    //判断路径是否存在
    QString absFilePath = dir.absoluteFilePath(ui->lblName->text());
    //    qDebug() << "absFilePath is " << absFilePath;
    QDir dist_dir(absFilePath);
    if(dist_dir.exists()){
        ui->tips->setText("project has exists, change path or name!");
        return false;
    }
    ui->tips->setText("");
    return QWizardPage::isComplete();

}

void ProSetPage::GetProSettings(QString &name, QString &path)
{
    name = ui->lblName->text();
    path = ui->lblPath->text();
}

void ProSetPage::on_pushButton_clicked()
{
    QFileDialog file_dialog;
    file_dialog.setFileMode(QFileDialog::Directory);
    file_dialog.setWindowTitle("选择导入的文件夹");
    auto path = QDir::currentPath();
    file_dialog.setDirectory(path);
    file_dialog.setViewMode(QFileDialog::Detail);
    QStringList fileNames;
    if (file_dialog.exec()){
        fileNames = file_dialog.selectedFiles();
    }
    if(fileNames.length() <= 0){
        return;
    }
    QString import_path = fileNames.at(0);
    qDebug() << "import_path is " << import_path ;
    ui->lblPath->setText(import_path);
}

